package com.QK.cnstudy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.QK.cnstudy.adapter.TestAdapter;
import com.QK.cnstudy.bean.Quiz;
import com.QK.cnstudy.bean.Subject;
import com.QK.cnstudy.db.DBManager;
import com.QK.cnstudy.entity.Work;
import com.QK.cnstudy.entity.WrongSubject;

public class TestSelectActivity extends Activity implements OnItemClickListener {
    private TestAdapter adapter;
    private ListView listView = null;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    private DBManager dbManager;

    public final int DIALOG_NO_WRONG = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_select);
        dbManager = CnStudyApp.app.dbManager;
        getView();
        setListViewData();
        adapter = new TestAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void setListViewData() {
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("title", "当前学习测试");
        map1.put("desc", "测试正在学习的可以的词义");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("title", "年级测试");
        map2.put("desc", "测试整个年级的课文的词义");

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("title", "错题测试");
        map3.put("desc", "实时获取当前教材版本的全国易错词义");

        list.add(map1);
        list.add(map2);
        list.add(map3);

    }

    public void getView() {
        listView = (ListView) findViewById(R.id.test_list);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Map<String, Object> map = list.get(arg2);
        String title = (String) map.get("title");
        Intent intent;
        if (title == "当前学习测试") {
            int curStudyWorkId = dbManager.getCurStudyWorkId();
            if (curStudyWorkId == -2) {
                Toast.makeText(this, "查询数据库失败", Toast.LENGTH_SHORT).show();
                return;
            }
            if (curStudyWorkId == -1) {
                Toast.makeText(this, "还未曾进行学习", Toast.LENGTH_SHORT).show();
                return;
            }
            intent = new Intent(this, SubjectActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            int workId = curStudyWorkId;
            Work work = dbManager.getWorkById(workId);
            Quiz quiz = work.getRandomQuiz(dbManager.getSqLiteDatabase());
            Bundle bundle = new Bundle();
            bundle.putSerializable("quiz", quiz);
            intent.putExtras(bundle);
            startActivity(intent);
        } else if (title == "单元测试") {

        } else if (title == "错题测试") {
            List<WrongSubject> wrongSubjects = dbManager.getWrongSubjects();
            if (wrongSubjects.size() == 0) {
                showDialog(DIALOG_NO_WRONG);
                return;
            } else {
                Quiz quiz = new Quiz();
                List<Subject> subjects = new ArrayList<Subject>();
                for (int i = 0; i < wrongSubjects.size(); i++) {
                    subjects.add(new Subject(wrongSubjects.get(i)));
                }
                quiz.setSubjectNum(subjects.size());
                quiz.setSubjects(subjects);
                Bundle bundle = new Bundle();
                bundle.putSerializable("quiz", quiz);
                intent = new Intent(this, SubjectActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtras(bundle);
                startActivity(intent);
            }

        }

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_NO_WRONG:
            dialog = new AlertDialog.Builder(this).setMessage("你还没有错题哦").setTitle("提示")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).create();
            break;
        }
        return dialog;

    }

}
